<?php
/**
 * Date: 2024.8
 * Author: kahn
 */
namespace service\vip;

use models\vip\GoodsModel;
use models\vip\SaleModel;
use models\vip\UserModel as VipUserModel;

class VipService
{
    static function createVip($userId, $goodsId): bool
    {

        $goods = GoodsModel::find($goodsId);
        if($goods->isEmpt() || $goods->status == 0){
            \Log::error("ceratevip, cannot find goods $userId, $goodsId");
            return false;
        }
        $days = $goods['days'];

        $vip = VipUserModel::vip($userId);
        if ($vip->isEmpty()) {
            $vip->userId = $userId;
            $vip->vipTime = time() + $days * 86400;;
        }else{
            $vip->status = 1;
            $vip->vipTime = ($vip->status == 1 && $vip->vipTime > time() ? $vip->vipTime : time()) +  + $days * 86400;
        }

        if (!$vip->save()) {
            \Log::error("create/update vip relation error", $vip->toArray());
            return false;
        }
        return true;
    }

    /**
     * @param $userId
     * @return bool
     */
    static function isVip($userId): bool
    {
        return VipUserModel::isVip($userId);
    }

    static public function goodsList(): ?array
    {
        $gList = GoodsModel::list();

        $prices = [];
        foreach ($gList as &$item) {
            $prices[$item['id']] = $item['price'];
            $item['saleId'] = 0;
            $item['discount'] = 0;
        }

        $sList = SaleModel::list();
        //计算每个商品对应的促销信息
        $sales = [];
        foreach ($sList as $s){
            $saleId = $s['id'];
            $goodsId = $s['goodsId'];
            if($goodsId == 0){
                foreach ($prices as $goodsId=>$price){
                    $discount = self::_discount($s['type'], $s['discount'], $price);
                    if(!isset($sales[$goodsId]) || $sales[$goodsId]['discount'] < $discount){
                        $sales[$goodsId] = ['saleId'=>$saleId, 'discount'=>$discount];
                    }
                }
            }elseif(isset($prices[$goodsId])){
                $discount = self::_discount($s['type'], $s['discount'], $prices[$goodsId]);
                if(!isset($sales[$goodsId]) || $sales[$goodsId]['discount'] < $discount){
                    $sales[$goodsId] = ['saleId'=>$saleId, 'discount'=>$discount];
                }
            }
        }

        foreach ($gList as &$item){
            $goodsId = $item['id'];
            if(isset($sales[$goodsId])){
                $item['saleId'] = $sales[$goodsId]['saleId'];
                $item['discount'] = $sales[$goodsId]['discount'];
            }
        }

        return $gList;
    }

    public static function _discount($type, $discount, $price): float
    {
        if ($type == 1){
            return $discount;
        }else{
            return ceil((10-$discount)/10*$price);
        }
    }

    /**
     * 优惠券是否可用
     * @param $sale
     * @param $goodsId
     * @return bool
     */
    public static function saleCanUse($sale, $goodsId): bool
    {
        return !$sale->isEmpty()
            && $sale->status == 1
            && $sale->startTime < time()
            && $sale->endTime > time()
            && ($sale->goodsId == 0 || $sale->goodsId == $goodsId);
    }

}